package com.guli.mall.wms.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.mall.wms.entity.PurchaseDetailEntity;
import com.guli.mall.wms.entity.vo.PurchaseDetailWithSkuNameVo;
import com.guli.mall.wms.entity.vo.PurchaseWithPurchaseDetailListVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;

import com.guli.mall.wms.entity.PurchaseEntity;
import com.guli.mall.wms.service.PurchaseService;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.R;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;


/**
 * 采购信息
 *
 * @author 罗俊华
 * @email 1007052116@qq.com
 * @date 2020-09-29 21:37:28
 */
@RestController
@RequestMapping("wms/purchase")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;


    @GetMapping("/list/{currentPage}/{size}")
    @ApiOperation("根据当前页和每页显示的记录数来获取purchase分页列表")
    public R list(@PathVariable("currentPage") Integer currentPage, @PathVariable("size") Integer size) {

        PageUtils<PurchaseEntity> page = purchaseService.queryPage(currentPage,size);

        /*for (PurchaseEntity purchaseEntity : page.getList()) {
            System.out.println(purchaseEntity);
        }*/

//        return R.ok().put("page", JSON.toJSONString(page));
        return R.ok().put("page", page);
    }



    @GetMapping("/info/{id}")
    @ApiOperation("根据id获取Purchase")
    public R info(@PathVariable("id") Long id) {
            PurchaseEntity purchase = purchaseService.getById(id);

        return R.ok().put("purchase", purchase);
    }

    @PostMapping("/save")
    @ApiOperation("插入Purchase")
    public R save(@RequestBody PurchaseEntity purchase) {
            purchaseService.save(purchase);

        return R.ok();
    }

    @PostMapping("/update")
    @ApiOperation("根据id更新Purchase")
    public R update(@RequestBody PurchaseEntity purchase) {
            purchaseService.updateById(purchase);

        return R.ok();
    }

    @PutMapping("/deleteBatch")
    @ApiOperation("根据id批量删除Purchase")
    public R delete(@RequestBody Long[] ids) {
            purchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @PostMapping("/searchPurchaseMenu/{currentPage}/{size}")
    @ApiOperation("搜索采购单，可以根据采购单id，采购人id（都用id表示），或者采购人名字来搜索")
    public R searchPurchaseMenu(@RequestBody PurchaseEntity purchaseEntity,
                                @PathVariable("currentPage") Integer currentPage,
                                @PathVariable("size") Integer size){

        Page<PurchaseEntity> page =  purchaseService.searchPurchaseMenu(purchaseEntity,currentPage,size);
        return R.ok().put("page",page);
    }

    @PostMapping("/createPurchasingMenuAndMergeToIt")
    @ApiOperation("为当前的采购需求创建一个新的采购单，并将采购需求合并到这个新的采购单中")
    public R createPurchasingMenuAndMergeToIt(@Valid @NotEmpty @RequestBody List<PurchaseDetailEntity> purchaseDetailEntityList){

        PurchaseEntity purchaseEntity = purchaseService.createPurchasingMenuAndMergeToIt(purchaseDetailEntityList,null);

        return R.ok().put("purchase",purchaseEntity);
    }

    // TODO 添加一个功能和 createPurchasingMenuAndMergeToIt controller 类似的方法，只不过是要绑定采购订单，而不是新建采购单
    @PostMapping("/bindPurchaseDetailToPurchaseId")
    @ApiOperation("将purchaseDetail采购需求绑定到采购表上")
    public R bindPurchaseDetailToPurchaseId(@Valid @NotEmpty @RequestBody List<PurchaseDetailEntity> purchaseDetailList){

        Long purchaseId = purchaseDetailList.get(0).getPurchaseId();

        PurchaseEntity purchaseEntity = new PurchaseEntity();

        purchaseEntity.setId(purchaseId);

        PurchaseEntity purchasingMenuAndMergeToIt = purchaseService.createPurchasingMenuAndMergeToIt(purchaseDetailList, purchaseEntity);

        return R.ok().put("purchase",purchasingMenuAndMergeToIt);
    }



    @GetMapping("/buyerGetPurchaseMenuList/{currentPage}/{size}/{buyerId}")
    @ApiOperation("采购员根据采购员id获取他的采购单列表")
    public R buyerGetPurchaseMenuList(@PathVariable("currentPage")Long currentPage,
                                      @PathVariable("size")Integer size,
                                      @PathVariable("buyerId")Long buyerId){

        Page<PurchaseEntity> page = purchaseService.buyerGetPurchaseMenuList(currentPage,size,buyerId);

        return R.ok().put("page",page);
    }

    @PostMapping("/getPurchaseMenu")
    @ApiOperation("采购人员领取采购单")
    public R getPurchaseMenu(@RequestBody  List<Long> purchaseIdList){

        purchaseService.getPurchaseMenu(purchaseIdList);

        return R.ok();
    }

    @PostMapping("/submitPurchasingResult")
    @ApiOperation("采购人员提交采购结果")
    public R submitPurchasingResult(@RequestBody PurchaseWithPurchaseDetailListVo purchaseWithPurchaseDetailListVo){

        purchaseService.submitPurchasingResult(purchaseWithPurchaseDetailListVo);

        return R.ok();
    }

}
